<template>
    <base-page class="report_buy_sale_bill_detail_container" ref="JxcBuySaleBillDetailPage" :options="pageOptions">
        <template v-slot:right_search='{searchForm}'>
            <div class='search_item'>
                <el-select v-model="searchForm.audit_state" placeholder="审核状态"
                           class="w100" clearable @change="refresh(true)"
                           style="margin-right: 5px;">
                    <el-option label="审核状态" value=""/>
                    <el-option label="待审核" value="0"/>
                    <el-option label="已审核" value="1"/>
                </el-select>
            </div>
            <div class='search_item'>
                <vSelectBottom v-model="searchForm.party_list" type="custom"
                               @selectEvent="handleBottomEvent" :options="partyOptions"
                               style="width: 200px;" />
            </div>
            <div class='search_item'>
                <vSelectBottom v-model="searchForm.goods_list" type="custom"
                               @selectEvent="handleBottomEvent" :options="goodsOptions"
                               style="width: 200px;" />
            </div>
            <div class='search_item'>
                <div>购销日期</div>
                <el-date-picker v-model="searchForm.bill_date" type="daterange" range-separator="至" @change="refresh(true)"
                                start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
            </div>
        </template>
    </base-page>
</template>

<script>
export default {
    name: "JxcBuySaleBillDetail",
    components: {
        BasePage: () => import('@common/views/common/BasePage.vue'),
    },
    data() {
        const _this = this;
        const baseExportInfo = {
            exportMinWidth: 20,
        };
        return {
            partyOptions: {
                url: _this.$api.API_FindBaseRelatedPartys,
                params(query) {
                    return $.extend({}, query, {
                        is_group: 0,
                    });
                },
                multiple: true,
                placeholder: '往来单位选择',
                props: {
                    value: "sid",
                    label: 'display',
                },
                hideBottomAction: true,
            },
            goodsOptions: {
                url: _this.$api.API_FindJxcGoodss,
                multiple: true,
                placeholder: '物料选择',
                props: {
                    value: "sid",
                    label: 'name',
                },
                hideBottomAction: true,
            },
            pageOptions: {
                url: _this.$api.API_FindBuySaleBillDetailReport,
                initSearchForm(searchForm) {
                    const partyList = [], goodsList = [];
                    if (_this.$utils.isNotEmpty(_this.$route.query.goods_sid)) {
                        goodsList.push(_this.$route.query.goods_sid);
                    }
                    return $.extend({}, searchForm, {
                        party_list: partyList,
                        goods_list: goodsList,
                    });
                },
                header: {
                    toolbar_buttons: [
                        {
                            id: 'refresh',
                            icon: 'el-icon-refresh-right',
                            show: true,
                            method: () => {
                                _this.refresh(true);
                            },
                        },
                        {
                            id: 'export',
                            icon: "el-icon-download",
                            label: '导出',
                            class: 'export_btn',
                            show: true,
                            method: () => {
                                _this.handleExport();
                            },
                        },
                    ],
                },
                table: {
                    ref: 'JxcBuySaleBillDetailTable',
                    columnField: {
                        type_name: '类型',
                        party_name: '往来单位',
                        bill_code: '单据编号',
                        bill_date: '购销日期',
                        goods_code: '物料编号',
                        goods_name: '物料名称',
                        goods_spec: '物料规格',
                        goods_model: '物料型号',
                        unit_name: '单位',
                        price: '单价',
                        qty: '数量',
                        tax_rate: '税率',
                        total_amount: '金额',
                        audit_state: '审核状态',
                    },
                    columnFormatter:{
                        audit_state:row => {
                            if (_this.$utils.isNotEmpty(row.audit_state) && row.audit_state === 1) {
                                return '已审核';
                            }
                            return '待审核';
                        },
                    },
                    export: {
                        type_name: {
                            exportMinWidth: 10
                        },
                        party_display: {
                            exportMinWidth: 15
                        },
                        bill_code: {
                            ...baseExportInfo
                        },
                        bill_date: {
                            ...baseExportInfo
                        },
                        goods_code: {
                            ...baseExportInfo
                        },
                        goods_name: {
                            ...baseExportInfo
                        },
                        goods_spec: {
                            ...baseExportInfo
                        },
                        goods_model: {
                            ...baseExportInfo
                        },
                        unit_name: {
                            ...baseExportInfo
                        },
                        price: {
                            ...baseExportInfo,
                            isAmount: true,
                            exportDataFormat: '#,##0.000',
                        },
                        qty: {
                            ...baseExportInfo,
                            isAmount: true,
                            exportDataFormat: '#,##0.000',
                            exportSum: true,
                        },
                        tax_rate: {
                            ...baseExportInfo,
                            isNumber: true,
                            exportDataFormat: '0.00%',
                        },
                        total_amount: {
                            ...baseExportInfo,
                            isAmount: true,
                            exportDataFormat: '#,##0.000',
                            exportSum: true,
                        },
                        audit_state: {
                            ...baseExportInfo
                        },
                    },
                },
            },
        };
    },
    methods: {
        refresh(query) {
            this.$refs.JxcBuySaleBillDetailPage && this.$refs.JxcBuySaleBillDetailPage.getList(query);
        },
        handleBottomEvent(event) {
            console.log(event)
            if (['clear', 'visibleChange', 'removeTag'].indexOf(event.type) !== -1) {
                if (event.type === 'visibleChange' && event.value) {
                    return;
                }
                this.refresh(true);
            }
        },
        handleExport() {
            if (this.$refs.JxcBuySaleBillDetailPage) {
                this.$refs.JxcBuySaleBillDetailPage.handleExportBase({
                    titleField(list) {
                        return list.map(item => {
                            const obj = $.extend({}, item);
                            if (item.value === '_index') {
                                obj.value = 'index';
                                obj.formula = '1';
                            }
                            return obj;
                        });
                    },
                }, '购销清单');
            }
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.report_buy_sale_bill_detail_container {
}
</style>
